﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
namespace BISPOS
{
    
    public partial class EditCustomer_SelectCustomerForm : DevExpress.XtraEditors.XtraForm
    {
        EditCustomerUserControl editCustomerUserControl;
        List<Customer> customers;
        public EditCustomer_SelectCustomerForm(EditCustomerUserControl _editCustomerUserControl, List<Customer> _customers)
        {
            InitializeComponent();
            editCustomerUserControl = _editCustomerUserControl;
            customers = _customers;
            customerListlinqServerModeSource.QueryableSource = customers.AsQueryable();
        }

        private void allCustomersListBoxControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Text = "เลือกลูกค้า - " + allCustomerListBoxControl.SelectedItems.OfType<Customer>().FirstOrDefault().CodeAndName ;
        }

        private void EditCustomer_SelectCustomerForm_Shown(object sender, EventArgs e)
        {
            searchTextEdit.Focus();
        }

        private void searchTextEdit_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                if (allCustomerListBoxControl.SelectedIndex != 0)
                {
                    try { allCustomerListBoxControl.SelectedIndex -= 1; }
                    catch { }
                }
            }
            if (e.KeyCode == Keys.Down)
            {
                try { allCustomerListBoxControl.SelectedIndex += 1; }
                catch { }
            }
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    this.editCustomerUserControl.ChangeCustomer(allCustomerListBoxControl.SelectedItems.OfType<Customer>().FirstOrDefault());
                    this.Close();
                }
                catch
                {

                }
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void searchTextEdit_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                customerListlinqServerModeSource.QueryableSource =  customers.Where(x => x.Name.Contains(searchTextEdit.Text)|| x.Code.Contains(searchTextEdit.Text)).AsQueryable();
                customerListlinqServerModeSource.Reload();
            }
            catch { }
        }



    }
}